package com.atguigu.advanced.jvm;

/**
 * Package: com.atguigu.advanced.jvm
 *
 * @Description: 测试栈内存溢出异常：StackOverflowError
 * @Author xiongkunYang
 * @Create 2025/5/16 16:59
 */

public class Test01 {
    private static long count = 0;

    public static void main(String[] args) {
        try {
            // 调用递归方法，不设置退出条件
            recursiveMethod();
        } catch (Throwable e) {
            System.err.println("StackOverflowError" + e.getMessage() + e.getCause());

            System.out.println("方法调用了" + count + "次");
            // 退出程序
            System.exit(1);
        }
    }

    private static void recursiveMethod() {
        count++;
        if (count == Long.MAX_VALUE) {
            return;
        }
        // 无限递归会导致StackOverflowError
        recursiveMethod();
    }

}